#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <atomic>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    enum fileType
    {
        IMAGE,
        TEXT
    };
    class timeUtil
    {
    public:
        static std::string GetTimeStamp()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec);
        }
        // 获得毫秒时间戳
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }
    };

    class fileUtil
    {
    public:
        static bool IsFileExists(const std::string &path_name)
        {
            struct stat st;
            if (stat(path_name.c_str(), &st) == 0)
            {
                // 获取属性成功，文件已经存在
                return true;
            }

            return false;
        }

        static std::string getSuffix(const std::string path_name)
        {
            auto pos = path_name.find_last_of('.');
            std::string suffix = path_name.substr(pos + 1);
            return suffix;
        }

        static bool readFile(const std::string &path_name, std::string *data)
        {
            (*data).clear();
            std::ifstream in(path_name);
            if (!in.is_open())
            {
                return false;
            }
            data->assign(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
            in.close();
            return true;
        }
    };

    class stringUtil
    {
    public:
        static void getContentType(const std::string path_name, std::string *content_type)
        {
            std::string suffix = fileUtil::getSuffix(path_name);
            if (suffix == "jpg")
            {
                *content_type = "application/x-jpg;";
            }
            else if (suffix == "png")
            {
                *content_type = "application/x-png;";
            }
            else if (suffix == "css")
            {
                *content_type = "text/css;";
            }
            else if (suffix == "js")
            {
                *content_type = "application/x-javascript;";
            }
            else
            {
            }
        }

        static std::string getBookType(const std::string &type)
        {
            if (type == "xuanhuan")
            {
                return "玄幻";
            }
            else if (type == "qihuan")
            {
                return "奇幻";
            }
            else if (type == "wuxia")
            {
                return "武侠";
            }
            else if (type == "xianxia")
            {
                return "仙侠";
            }
            else if (type == "dushi")
            {
                return "都市";
            }
            else if (type == "xianshi")
            {
                return "现实";
            }
            else if (type == "junshi")
            {
                return "军事";
            }
            else if (type == "lishi")
            {
                return "历史";
            }
            else if (type == "youxi")
            {
                return "游戏";
            }
            else if (type == "tiyu")
            {
                return "体育";
            }
            else if (type == "kehuan")
            {
                return "科幻";
            }
            else if (type == "zhutianwuxian")
            {
                return "诸天无限";
            }
            else if (type == "xuanyi")
            {
                return "悬疑";
            }
            else if (type == "qingxiaoshuo")
            {
                return "轻小说";
            }
            else
            {
                return "";
            }
        }

        static std::string getBookTypeReverse(const std::string key)
        {
            if (key == "玄幻")
            {
                return "xuanhuan";
            }
            else if (key == "奇幻")
            {
                return "qihuan";
            }
            else if (key == "科幻")
            {
                return "kehuan";
            }
            else if (key == "悬疑")
            {
                return "xuanyi";
            }
            else if (key == "武侠")
            {
                return "wuxia";
            }
            else if (key == "仙侠")
            {
                return "xianxia";
            }
            else if (key == "都市")
            {
                return "dushi";
            }
            else if (key == "现实")
            {
                return "xianshi";
            }
            else if (key == "历史")
            {
                return "lishi";
            }
            else if (key == "军事")
            {
                return "junshi";
            }
            else if (key == "游戏")
            {
                return "youxi";
            }
            else if (key == "体育")
            {
                return "tiyu";
            }
            else if (key == "轻小说")
            {
                return "qingxiaoshuo";
            }
            else if (key == "诸天无限")
            {
                return "zhutianwuxian";
            }
            else
            {
                return "";
            }
        }

        /*************************************
         * str: 输入型，目标要切分的字符串
         * target: 输出型，保存切分完毕的结果
         * sep: 指定的分割符
         * **********************************/
        static void
        splitString(const std::string &str, std::vector<std::string> *target, const std::string &sep)
        {
            // boost split
            boost::split((*target), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };

}